using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class NumberManager
{
    private HashSet<Number> Numbers;

    public NumberManager()
    {
        Numbers = new HashSet<Number>(300);
        outputBuilder = new StringBuilder();
 
        
        
        Debug.Log(Numbers.Count);
    }


    private StringBuilder outputBuilder;

    public string Output()
    {
        outputBuilder.Clear();
        foreach (var item in Numbers)
        {
            outputBuilder.Append($"{item} ");
        }

        outputBuilder.AppendLine();
        outputBuilder.Append($"{Numbers.Count}注");

        return outputBuilder.ToString();
    }

    private HashSet<Number> resourlt = new HashSet<Number>();
    public void StartLottery(cfg.gamedata data)
    {
        Numbers.Clear();
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                for (int k = 0; k < 10; k++)
                {
                    Numbers.Add(new Number(i, j, k));
                }
            }
        }
        resourlt.Clear();
        foreach (var item in Numbers)
        {
            if (IsSuccess(data,item))
            {
                resourlt.Add(item);
            }
        }
        Numbers.Clear();
        foreach (var VARIABLE in resourlt)
        {
            Numbers.Add(VARIABLE);
        }
    }

    public void StartChoiceLottery(cfg.gamedata data)
    {
        Numbers.Clear();
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                for (int k = 0; k < 10; k++)
                {
                    Numbers.Add(new Number(i, j, k));
                }
            }
        }
        resourlt.Clear();
        foreach (var item in Numbers)
        {
            if (IsChoiceSuccess(data, item))
            {
                resourlt.Add(item);
            }
        }
        Numbers.Clear();
        foreach (var VARIABLE in resourlt)
        {
            Numbers.Add(VARIABLE);
        }
    }
    bool IsChoiceSuccess(cfg.gamedata data, Number item)
    {
        if (!FilterNumber(item, data.Condition1, 1, 2))
        {
            return false;
        }
        if (!FilterNumber(item, data.Condition2, 0, 1, 2))
        {
            return false;
        }

        if (!FilterNumber(item, data.Condition3, 1, 2, 0))
        {
            return false;
        }
        if (!FilterNumber(item, data.Condition4,1, 2, 3))
        {
            return false;
        }
        if (!FilterNumber(item, data.Condition5,  0,1, 2))
        {
            return false;
        }

        if (!FilterNumber(item, data.Condition6, 1, 2))
        {
            return false;
        }

        if (!FilterNumber(item, data.Condition7, 1, 2, 3))
        {
            return false;
        }
        // if (!FilterNumber(item,data.Condition3,1,2,3))
        // {
        //     return false;
        // }
        if (!FilterNumber(item, data.Condition9, 0))
        {
            return false;
        }
        if (!FilterNumber(item, data.Condition10, 1, 2, 3))
        {
            return false;
        }

        if (!FilterNumber(item, data.Condition11, 1, 2, 3))
        {
            return false;
        }
        if (!FilterNumber(item, data.Condition12, 1, 2, 0))
        {
            return false;
        }
        if (!FilterNumber(item, data.Condition13, 1, 2, 0))
        {
            return false;
        }

        if (!FilterNumber(item, data.Condition14, 1, 2, 0))
        {
            return false;
        }

        //if (FilterCompositeNumber(item, data.Condition15))
        //{
        //    return false;
        //}

        return true;
    }
    bool IsSuccess(cfg.gamedata data,Number item)
    {
        if (!FilterNumber(item, data.Condition1, 1, 2))
        {
            return false;
        }
        if (!FilterNumber(item, data.Condition2, 0, 1, 2))
        {
            return false;
        }

        if (!FilterNumber(item, data.Condition3, 1, 2, 0))
        {
            return false;
        }
        if (!FilterNumber(item, data.Condition4, 1, 2, 3))
        {
            return false;
        }
        if (!FilterNumber(item, data.Condition5, 0, 1, 2))
        {
            return false;
        }

        if (!FilterNumber(item, data.Condition6, 1, 2, 0))
        {
            return false;
        }

        if (!FilterNumber(item, data.Condition7, 1, 2, 3))
        {
            return false;
        }
        // if (!FilterNumber(item,data.Condition3,1,2,3))
        // {
        //     return false;
        // }
        //if (!FilterNumber(item, data.Condition9, 0))
        //{
        //    return false;
        //}
        if (!FilterNumber(item, data.Condition10, 1, 2, 3))
        {
            return false;
        }

        if (!FilterNumber(item, data.Condition11, 1, 2, 3))
        {
            return false;
        }
        if (!FilterNumber(item, data.Condition12, 1, 2, 0))
        {
            return false;
        }
        if (!FilterNumber(item, data.Condition13, 1, 2, 0))
        {
            return false;
        }

        if (!FilterNumber(item, data.Condition14, 1, 2, 0))
        {
            return false;
        }

        //if (FilterCompositeNumber(item, data.Condition15))
        //{
        //    return false;
        //}

        return true;
    }
    HashSet<Number> nums = new HashSet<Number>();
    public bool FilterCompositeNumber(Number n, string condition)
    {
        if (string.IsNullOrEmpty(condition) || condition.Equals(""))
        {
            return true;
        }

        HashSet<int> cond1Nums = new HashSet<int>();
        HashSet<int> cond2Nums = new HashSet<int>();
        HashSet<int> cond3Nums = new HashSet<int>();

        string[] conds = condition.Split('/');
        int startIndex = 0;


        for (int i = 0; i < conds[startIndex].Length; i++)
        {
            var c = conds[startIndex].Substring(i, 1);
            cond1Nums.Add(int.Parse(c));
        }
        startIndex++;

        for (int i = 0; i < conds[startIndex].Length; i++)
        {
            var c = conds[startIndex].Substring(i, 1);
            cond2Nums.Add(int.Parse(c));
        }
        startIndex++;

        for (int i = 0; i < conds[startIndex].Length; i++)
        {
            var c = conds[startIndex].Substring(i, 1);
            cond3Nums.Add(int.Parse(c));
        }
        startIndex++;


        nums.Clear();

        foreach (var item1 in cond1Nums)
        {
            foreach (var item2 in cond2Nums)
            {
                foreach (var item3 in cond3Nums)
                {
                    nums.Add(new Number(item1, item2, item3));
                }
            }
        }

        return nums.Contains(n);
    }



    public bool FilterNumber(Number n,string condition,params int[] condCount)
    {
        if (string.IsNullOrEmpty(condition) || condition.Equals(""))
        {
            return true;
        }
        HashSet<int> condNums = new HashSet<int>();
        for (int i = 0; i < condition.Length; i++)
        {
            var c = condition.Substring(i, 1);

            condNums.Add(int.Parse(c));
        }

        int count = 0;
        /// 去重
        HashSet<int> hashSet = new HashSet<int>();
        hashSet.Add(n.x);
        hashSet.Add(n.y);
        hashSet.Add(n.z);

        foreach (var item in hashSet)
        {
            if (condNums.Contains(item))
            {
                count++;
            }
        }

        return condCount.Contains(count);
    }
}